package com.haiheng.common.constant

import android.os.Environment
import android.text.TextUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.haiheng.library.log.LogUtils
import com.haiheng.library_base.toast.Utils
import com.haiheng.library_base.utils.DateUtils

import com.haiheng.library_base.utils.SPUtils
import com.haiheng.library_base.utils.SystemUtil
import com.ys.rkapi.MyManager
import java.util.*

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/5/5 0005
 * @Version: 1.0
 */
class AppConstant {
    companion object{

        val countDownTime = 180
        val defaultReportInterval = 10 //单位s
        const val INITIAL_PASSWORD = "111111"
//        const val DEFAUL_LIBRARY_CODE = "CN-518000-HHLIB"
        const val DEFAUL_LIBRARY_CODE = "CN-350003-FJLIB"
        const val BASE_WEB_VIEW_URL = "https://rsp.fjlib.net:3002/phone"

        const val SP_TOKEN = "SP_TOKEN"
        const val SP_ACCOUNT = "SP_ACCOUNT"
        const val SP_PASSWORD = "SP_PASSWORD"
        const val SP_IS_SAVE_PASSWORD = "SP_IS_SAVE_PASSWORD"
        const val SP_USER_LOGIN = "SP_USER_LOGIN"

        const val SP_WEB_VIEW_URL = "webViewUrl"
        const val SP_ADMIN_PASSWORD = "SP_ADMIN_PASSWORD"
        const val SP_OPEN_OFFLINE_MODE   = "SP_OPEN_OFFLINE_MODE"
        const val SP_WEBVIEW_SIZE  = "SP_WEBVIEW_SIZE"
        const val SP_IS_EXT  = "SP_IS_EXT"
        const val SP_LIBRARYCODE = "SP_LIBRARYCODE"
        const val SP_MACHINE_NAME = "SP_MACHINE_NAME"
        const val SP_BOOK_CAR_UUID = "SP_BOOK_CAR_UUID"
        const val SP_BOOK_CAR_NAME = "SP_BOOK_CAR_NAME"
        const val SP_REPORT_INTERVAL= "SP_REPORT_INTERVAL"
        const val SP_SCHEDULED_TASK  = "SP_SCHEDULED_TASK"//定时任务
        const val SP_MQTT_URL = "SP_MQTT_URL"
        const val SP_MQTT_NAME = "SP_MQTT_NAME"
        const val SP_MQTT_PASSWORD = "SP_MQTT_PASSWORD"
        const val SP_LAST_INVENTORY_TIME = "SP_LAST_INVENTORY_TIME"//最后一次盘点时间
        const val SP_LAST_SYN_TIME = "SP_LAST_SYN_TIME"//最后一次同步时间
        var TABALE_ONE = 1
        var TABALE_TOW = 2
        var TABALE_THRESS = 3

        //mqtt topic
        var TOPIC_COMMANDS_PRE = "\$oc/devices/${getMachineUuid()}/sys/commands"
        var TOPIC_COMMANDS = "${TOPIC_COMMANDS_PRE}/#"
        var TOPIC_COMMANDS_RESPONSE = "\$oc/devices/${getMachineUuid()}/sys/commands/response"
        var TOPIC_PROPERTIEST_PRE = "\$oc/devices/${getMachineUuid()}/sys/properties/set"
        var TOPIC_PROPERTIEST = "${TOPIC_PROPERTIEST_PRE}/#"
        var TOPIC_PROPERTIEST_RESPONSE = "\$oc/devices/${getMachineUuid()}/sys/properties/set/response"
        var TOPIC_EVENTS_DOWN = "\$oc/devices/${getMachineUuid()}/sys/events/down"
        var TOPIC_EVENTS_UP= "\$oc/devices/${getMachineUuid()}/sys/events/up"
        const val QOS = 1
        private const val RECONNECT_DELAY = 5000L // 5 seconds
        const val DEFAULD_PAGE_SIZE = 100;

        fun getReportInterval():Int{
            return SPUtils.getInstance().getInt(SP_REPORT_INTERVAL, defaultReportInterval)
        }
        fun getMachineInfo():String{
            var manage = MyManager.getInstance(Utils.getApp())
//            var name = "${manage.androidModle}-${manage.vendorID}-${manage.sn}+${manage.androidVersion}"
            var name = "${manage.sn}-${manage.androidVersion}"
            return name
        }

        fun getMqttName():String{
            return SPUtils.getInstance().getString(SP_MQTT_NAME, "devSyncTest")
        }

        fun getMqttPassword():String{
            return SPUtils.getInstance().getString(SP_MQTT_PASSWORD, "devSync123!Test")
        }

        fun getBrokerUrl():String{
//            return "mqtt://127.0.0.1:9310"
            return SPUtils.getInstance().getString(SP_MQTT_URL, "tcp://172.16.0.118:9310")
        }
        fun getClientId():String{
            var clienId = "${AppConstant.getMachineUuid()}_0_0_${
                DateUtils.formatDateTime(
                    Date(),
                    DateUtils.DATE_FORMAT_9)}"
            return clienId
        }
        fun getMachineUuid():String{
            var mac = SystemUtil.getMacAddress(Utils.getApp())
            var uuid = mac.replace(":","")
            LogUtils.e("uuid:$uuid")
//            return uuid
            return "F1F1F1F1F1F1"
        }

        fun getMachineName():String{
            return SPUtils.getInstance().getString(SP_MACHINE_NAME, getMachineInfo())
        }
        fun getLibraryCode():String{
            return SPUtils.getInstance().getString(SP_LIBRARYCODE,DEFAUL_LIBRARY_CODE)
        }
        fun getToken():String{
            var token = SPUtils.getInstance().getString(AppConstant.SP_TOKEN)
            return token
        }
        fun isExt():Boolean{
            var isExt =  SPUtils.getInstance().getBoolean(SP_IS_EXT,false)
            return isExt
        }

        fun setNoExt(){
            SPUtils.getInstance().put(SP_IS_EXT,false)
        }

        fun isOpenOfflineMode():Boolean{
            var isOpen =  SPUtils.getInstance().getBoolean(SP_OPEN_OFFLINE_MODE,false)
            return isOpen
        }
        fun getWebViewUrl():String{
            var url =  SPUtils.getInstance().getString(SP_WEB_VIEW_URL,BASE_WEB_VIEW_URL)
            return url
        }

        fun getAdminPassword():String{
            var password = SPUtils.getInstance().getString(SP_ADMIN_PASSWORD,INITIAL_PASSWORD)
            return password
        }

        fun getWebpageSize():Int{
            var webSize = SPUtils.getInstance().getInt(SP_WEBVIEW_SIZE,100)
            return webSize
        }

        fun isOpenLog() : Boolean{
            var isOpenLog =  SPUtils.getInstance().getBoolean(Keys.SWITCH_OPEN_LOG,true)
            return  isOpenLog
        }
        fun isOpenView() : Boolean{
            var isOpenView =  SPUtils.getInstance().getBoolean(Keys.SWITCH_DEBUG_VIEW,false)
            return  isOpenView
        }

        fun getLastInventoryTime():String{
            var time = SPUtils.getInstance().getString(SP_LAST_INVENTORY_TIME,"")
            return time
        }

        fun getLastSnyTime():String{
            var time = SPUtils.getInstance().getString(SP_LAST_SYN_TIME,"")
            return time
        }
        fun getCarUUid():String{
            return SPUtils.getInstance().getString(SP_BOOK_CAR_UUID, "")
        }
         fun getCarName():String{
            return SPUtils.getInstance().getString(SP_BOOK_CAR_NAME, "")
        }
    }

}